<?php
// var_dump(1);die;
ini_set("display_errors", "on");
require_once 'api_sdk/vendor/autoload.php';
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;

// 加载区域结点配置
Config::load();

/**
 * Class SmsDemo
 *
 * Created on 17/10/17.
 * 短信服务API产品的DEMO程序,工程中包含了一个SmsDemo类，直接通过
 * 执行此文件即可体验语音服务产品API功能(只需要将AK替换成开通了云通信-短信服务产品功能的AK即可)
 * 备注:Demo工程编码采用UTF-8
 */
class SmsDemo
{

    static $acsClient = null;
    public $sms_appkey = '';
    public $sms_appSecret = '';
    public $sms_register_template = '';
    public $sms_server_template = '';
    public $sms_sign_name = '';
    public function __construct(){
        //从config表获取信息
        $sms_appkey = db('Config')->where(['key'=>'sms_appkey'])->find()['value'];
        $sms_appSecret = db('Config')->where(['key'=>'sms_appSecret'])->find()['value'];
        $sms_register_template = db('Config')->where(['key'=>'sms_register_template'])->find()['value'];
        $sms_server_template = db('Config')->where(['key'=>'sms_server_template'])->find()['value'];
        $sms_sign_name = db('Config')->where(['key'=>'sms_sign_name'])->find()['value'];
        $this->sms_appkey = $sms_appkey;
        $this->sms_appSecret = $sms_appSecret;
        $this->sms_register_template = $sms_register_template;
        $this->sms_server_template = $sms_server_template;
        $this->sms_sign_name = $sms_sign_name;
    }

    /**
     * 取得AcsClient
     *
     * @return DefaultAcsClient
     */
    public function getAcsClient() {
        //产品名称:云通信流量服务API产品,开发者无需替换
        $product = "Dysmsapi";

        //产品域名,开发者无需替换
        $domain = "dysmsapi.aliyuncs.com";

        // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
        $accessKeyId = $this->sms_appkey; // AccessKeyId

        $accessKeySecret = $this->sms_appSecret; // AccessKeySecret

        // 暂时不支持多Region
        $region = "cn-hangzhou";

        // 服务结点
        $endPointName = "cn-hangzhou";


        if(static::$acsClient == null) {

            //初始化acsClient,暂不支持region化
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);

            // 增加服务结点
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);

            // 初始化AcsClient用于发起请求
            static::$acsClient = new DefaultAcsClient($profile);
        }
        return static::$acsClient;
    }

    /**
     * 发送短信
     * @return stdClass
     */
    public function sendSms($phone,$type=1,$code,$pass) {

        // 初始化SendSmsRequest实例用于设置发送短信的参数
        $request = new SendSmsRequest();
        if($type==1){
             // 必填，设置短信接收号码
            $request->setPhoneNumbers($phone);

            // 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
            $request->setSignName($this->sms_sign_name);

            // 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
            $request->setTemplateCode($this->sms_register_template);

            // 可选，设置模板参数, 假如模板中存在变量需要替换则为必填项
            $request->setTemplateParam(json_encode(Array(  // 短信模板中字段的值
                "veri"=>$code,
            )));
        }
        if($type==2){
             // 必填，设置短信接收号码
            $request->setPhoneNumbers($phone);

            // 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
            $request->setSignName($this->sms_sign_name);

            // 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
            $request->setTemplateCode($this->sms_server_template);

            // 可选，设置模板参数, 假如模板中存在变量需要替换则为必填项
            $request->setTemplateParam(json_encode(Array(  // 短信模板中字段的值
                "phone"=>$phone,
                "pass"=>$pass
            )));
        }
       

        // 可选，设置流水号
       // $request->setOutId("yourOutId");

        // 选填，上行短信扩展码（扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段）
       // $request->setSmsUpExtendCode("1234567");

        // 发起访问请求
        $acsResponse = static::getAcsClient()->getAcsResponse($request);

        return $acsResponse;

    }

   

}

// // 调用示例：
// set_time_limit(0);
// header('Content-Type: text/plain; charset=utf-8');

// $response = SmsDemo::sendSms();
// echo "发送短信(sendSms)接口返回的结果:\n";
// print_r($response);

// sleep(2);

// $response = SmsDemo::querySendDetails();
// echo "查询短信发送情况(querySendDetails)接口返回的结果:\n";
// print_r($response);
